home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Modules / regexpr.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-10  |  45.9 KB  |  2,137 lines

  1. /* regexpr.c
  2.  *
  3.  * Author: Tatu Ylonen <ylo@ngs.fi>
  4.  *
  5.  * Copyright (c) 1991 Tatu Ylonen, Espoo, Finland
  6.  *
  7.  * Permission to use, copy, modify, distribute, and sell this software
  8.  * and its documentation for any purpose is hereby granted without
  9.  * fee, provided that the above copyright notice appear in all copies.
  10.  * This software is provided "as is" without express or implied
  11.  * warranty.
  12.  *
  13.  * Created: Thu Sep 26 17:14:05 1991 ylo
  14.  * Last modified: Mon Nov  4 17:06:48 1991 ylo
  15.  * Ported to Think C: 19 Jan 1992 guido@cwi.nl
  16.  *
  17.  * This code draws many ideas from the regular expression packages by
  18.  * Henry Spencer of the University of Toronto and Richard Stallman of
  19.  * the Free Software Foundation.
  20.  *
  21.  * Emacs-specific code and syntax table code is almost directly borrowed
  22.  * from GNU regexp.
  23.  *
  24.  * Bugs fixed and lots of reorganization by Jeffrey C. Ollie, April
  25.  * 1997 Thanks for bug reports and ideas from Andrew Kuchling, Tim
  26.  * Peters, Guido van Rossum, Ka-Ping Yee, Sjoerd Mullender, and
  27.  * probably one or two others that I'm forgetting.
  28.  *
  29.  * $Id: regexpr.c,v 1.30 1999/04/10 15:46:56 guido Exp $ */
  30.  
  31. #include "Python.h"
  32. #include "regexpr.h"
  33. #include <assert.h>
  34. #include "protos/regexpr.h"
  35.  
  36. /* The original code blithely assumed that sizeof(short) == 2.  Not
  37.  * always true.  Original instances of "(short)x" were replaced by
  38.  * SHORT(x), where SHORT is #defined below.  */
  39.  
  40. #define SHORT(x) ((x) & 0x8000 ? (x) - 0x10000 : (x))
  41.  
  42. /* The stack implementation is taken from an idea by Andrew Kuchling.
  43.  * It's a doubly linked list of arrays. The advantages of this over a
  44.  * simple linked list are that the number of mallocs required are
  45.  * reduced. It also makes it possible to statically allocate enough
  46.  * space so that small patterns don't ever need to call malloc.
  47.  *
  48.  * The advantages over a single array is that is periodically
  49.  * realloced when more space is needed is that we avoid ever copying
  50.  * the stack. */
  51.  
  52. /* item_t is the basic stack element.  Defined as a union of
  53.  * structures so that both registers, failure points, and counters can
  54.  * be pushed/popped from the stack.  There's nothing built into the
  55.  * item to keep track of whether a certain stack item is a register, a
  56.  * failure point, or a counter. */
  57.  
  58. typedef union item_t
  59. {
  60.     struct
  61.     {
  62.         int num;
  63.         int level;
  64.         unsigned char *start;
  65.         unsigned char *end;
  66.     } reg;
  67.     struct
  68.     {
  69.         int count;
  70.         int level;
  71.         int phantom;
  72.         unsigned char *code;
  73.         unsigned char *text;
  74.     } fail;
  75.     struct
  76.     {
  77.         int num;
  78.         int level;
  79.         int count;
  80.     } cntr;
  81. } item_t;
  82.  
  83. #define STACK_PAGE_SIZE 256
  84. #define NUM_REGISTERS 256
  85.  
  86. /* A 'page' of stack items. */
  87.  
  88. typedef struct item_page_t
  89. {
  90.     item_t items[STACK_PAGE_SIZE];
  91.     struct item_page_t *prev;
  92.     struct item_page_t *next;
  93. } item_page_t;
  94.  
  95.  
  96. typedef struct match_state
  97. {
  98.     /* The number of registers that have been pushed onto the stack
  99.      * since the last failure point. */
  100.  
  101.     int count;
  102.  
  103.     /* Used to control when registers need to be pushed onto the
  104.      * stack. */
  105.     
  106.     int level;
  107.     
  108.     /* The number of failure points on the stack. */
  109.     
  110.     int point;
  111.     
  112.     /* Storage for the registers.  Each register consists of two
  113.      * pointers to characters.  So register N is represented as
  114.      * start[N] and end[N].  The pointers must be converted to
  115.      * offsets from the beginning of the string before returning the
  116.      * registers to the calling program. */
  117.     
  118.     unsigned char *start[NUM_REGISTERS];
  119.     unsigned char *end[NUM_REGISTERS];
  120.     
  121.     /* Keeps track of whether a register has changed recently. */
  122.     
  123.     int changed[NUM_REGISTERS];
  124.     
  125.     /* Structure to encapsulate the stack. */
  126.     struct
  127.     {
  128.         /* index into the curent page.  If index == 0 and you need
  129.          * to pop an item, move to the previous page and set index
  130.          * = STACK_PAGE_SIZE - 1.  Otherwise decrement index to
  131.          * push a page. If index == STACK_PAGE_SIZE and you need
  132.          * to push a page move to the next page and set index =
  133.          * 0. If there is no new next page, allocate a new page
  134.          * and link it in. Otherwise, increment index to push a
  135.          * page. */
  136.         
  137.         int index;
  138.         item_page_t *current; /* Pointer to the current page. */
  139.         item_page_t first; /* First page is statically allocated. */
  140.     } stack;
  141. } match_state;
  142.  
  143. /* Initialize a state object */
  144.  
  145. /* #define NEW_STATE(state) \ */
  146. /* memset(&state, 0, (void *)(&state.stack) - (void *)(&state)); \ */
  147. /* state.stack.current = &state.stack.first; \ */
  148. /* state.stack.first.prev = NULL; \ */
  149. /* state.stack.first.next = NULL; \ */
  150. /* state.stack.index = 0; \ */
  151. /* state.level = 1 */
  152.  
  153. #define NEW_STATE(state, nregs) \
  154. { \
  155.     int i; \
  156.     for (i = 0; i < nregs; i++) \
  157.     { \
  158.         state.start[i] = NULL; \
  159.         state.end[i] = NULL; \
  160.         state.changed[i] = 0; \
  161.     } \
  162.     state.stack.current = &state.stack.first; \
  163.     state.stack.first.prev = NULL; \
  164.     state.stack.first.next = NULL; \
  165.     state.stack.index = 0; \
  166.     state.level = 1; \
  167.     state.count = 0; \
  168.     state.level = 0; \
  169.     state.point = 0; \
  170. }
  171.  
  172. /* Free any memory that might have been malloc'd */
  173.  
  174. #define FREE_STATE(state) \
  175. while(state.stack.first.next != NULL) \
  176. { \
  177.     state.stack.current = state.stack.first.next; \
  178.     state.stack.first.next = state.stack.current->next; \
  179.     free(state.stack.current); \
  180. }
  181.  
  182. /* Discard the top 'count' stack items. */
  183.  
  184. #define STACK_DISCARD(stack, count, on_error) \
  185. stack.index -= count; \
  186. while (stack.index < 0) \
  187. { \
  188.     if (stack.current->prev == NULL) \
  189.         on_error; \
  190.     stack.current = stack.current->prev; \
  191.     stack.index += STACK_PAGE_SIZE; \
  192. }
  193.  
  194. /* Store a pointer to the previous item on the stack. Used to pop an
  195.  * item off of the stack. */
  196.  
  197. #define STACK_PREV(stack, top, on_error) \
  198. if (stack.index == 0) \
  199. { \
  200.     if (stack.current->prev == NULL) \
  201.         on_error; \
  202.     stack.current = stack.current->prev; \
  203.     stack.index = STACK_PAGE_SIZE - 1; \
  204. } \
  205. else \
  206. { \
  207.     stack.index--; \
  208. } \
  209. top = &(stack.current->items[stack.index])
  210.  
  211. /* Store a pointer to the next item on the stack. Used to push an item
  212.  * on to the stack. */
  213.  
  214. #define STACK_NEXT(stack, top, on_error) \
  215. if (stack.index == STACK_PAGE_SIZE) \
  216. { \
  217.     if (stack.current->next == NULL) \
  218.     { \
  219.         stack.current->next = (item_page_t *)malloc(sizeof(item_page_t)); \
  220.         if (stack.current->next == NULL) \
  221.             on_error; \
  222.         stack.current->next->prev = stack.current; \
  223.         stack.current->next->next = NULL; \
  224.     } \
  225.     stack.current = stack.current->next; \
  226.     stack.index = 0; \
  227. } \
  228. top = &(stack.current->items[stack.index++])
  229.  
  230. /* Store a pointer to the item that is 'count' items back in the
  231.  * stack. STACK_BACK(stack, top, 1, on_error) is equivalent to
  232.  * STACK_TOP(stack, top, on_error).  */
  233.  
  234. #define STACK_BACK(stack, top, count, on_error) \
  235. { \
  236.     int index; \
  237.     item_page_t *current; \
  238.     current = stack.current; \
  239.     index = stack.index - (count); \
  240.     while (index < 0) \
  241.     { \
  242.         if (current->prev == NULL) \
  243.             on_error; \
  244.         current = current->prev; \
  245.         index += STACK_PAGE_SIZE; \
  246.     } \
  247.     top = &(current->items[index]); \
  248. }
  249.  
  250. /* Store a pointer to the top item on the stack. Execute the
  251.  * 'on_error' code if there are no items on the stack. */
  252.  
  253. #define STACK_TOP(stack, top, on_error) \
  254. if (stack.index == 0) \
  255. { \
  256.     if (stack.current->prev == NULL) \
  257.         on_error; \
  258.     top = &(stack.current->prev->items[STACK_PAGE_SIZE - 1]); \
  259. } \
  260. else \
  261. { \
  262.     top = &(stack.current->items[stack.index - 1]); \
  263. }
  264.  
  265. /* Test to see if the stack is empty */
  266.  
  267. #define STACK_EMPTY(stack) ((stack.index == 0) && \
  268.                 (stack.current->prev == NULL))
  269.  
  270. /* Return the start of register 'reg' */
  271.  
  272. #define GET_REG_START(state, reg) (state.start[reg])
  273.  
  274. /* Return the end of register 'reg' */
  275.  
  276. #define GET_REG_END(state, reg) (state.end[reg])
  277.  
  278. /* Set the start of register 'reg'. If the state of the register needs
  279.  * saving, push it on the stack. */
  280.  
  281. #define SET_REG_START(state, reg, text, on_error) \
  282. if(state.changed[reg] < state.level) \
  283. { \
  284.     item_t *item; \
  285.     STACK_NEXT(state.stack, item, on_error); \
  286.     item->reg.num = reg; \
  287.     item->reg.start = state.start[reg]; \
  288.     item->reg.end = state.end[reg]; \
  289.     item->reg.level = state.changed[reg]; \
  290.     state.changed[reg] = state.level; \
  291.     state.count++; \
  292. } \
  293. state.start[reg] = text
  294.  
  295. /* Set the end of register 'reg'. If the state of the register needs
  296.  * saving, push it on the stack. */
  297.  
  298. #define SET_REG_END(state, reg, text, on_error) \
  299. if(state.changed[reg] < state.level) \
  300. { \
  301.     item_t *item; \
  302.     STACK_NEXT(state.stack, item, on_error); \
  303.     item->reg.num = reg; \
  304.     item->reg.start = state.start[reg]; \
  305.     item->reg.end = state.end[reg]; \
  306.     item->reg.level = state.changed[reg]; \
  307.     state.changed[reg] = state.level; \
  308.     state.count++; \
  309. } \
  310. state.end[reg] = text
  311.  
  312. #define PUSH_FAILURE(state, xcode, xtext, on_error) \
  313. { \
  314.     item_t *item; \
  315.     STACK_NEXT(state.stack, item, on_error); \
  316.     item->fail.code = xcode; \
  317.     item->fail.text = xtext; \
  318.     item->fail.count = state.count; \
  319.     item->fail.level = state.level; \
  320.     item->fail.phantom = 0; \
  321.     state.count = 0; \
  322.     state.level++; \
  323.     state.point++; \
  324. }
  325.  
  326. /* Update the last failure point with a new position in the text. */
  327.  
  328. #define UPDATE_FAILURE(state, xtext, on_error) \
  329. { \
  330.     item_t *item; \
  331.     STACK_BACK(state.stack, item, state.count + 1, on_error); \
  332.     if (!item->fail.phantom) \
  333.     { \
  334.         item_t *item2; \
  335.         STACK_NEXT(state.stack, item2, on_error); \
  336.         item2->fail.code = item->fail.code; \
  337.         item2->fail.text = xtext; \
  338.         item2->fail.count = state.count; \
  339.         item2->fail.level = state.level; \
  340.         item2->fail.phantom = 1; \
  341.         state.count = 0; \
  342.         state.level++; \
  343.         state.point++; \
  344.     } \
  345.     else \
  346.     { \
  347.         STACK_DISCARD(state.stack, state.count, on_error); \
  348.         STACK_TOP(state.stack, item, on_error); \
  349.         item->fail.text = xtext; \
  350.         state.count = 0; \
  351.         state.level++; \
  352.     } \
  353. }
  354.  
  355. #define POP_FAILURE(state, xcode, xtext, on_empty, on_error) \
  356. { \
  357.     item_t *item; \
  358.     do \
  359.     { \
  360.         while(state.count > 0) \
  361.         { \
  362.             STACK_PREV(state.stack, item, on_error); \
  363.             state.start[item->reg.num] = item->reg.start; \
  364.             state.end[item->reg.num] = item->reg.end; \
  365.             state.changed[item->reg.num] = item->reg.level; \
  366.             state.count--; \
  367.         } \
  368.         STACK_PREV(state.stack, item, on_empty); \
  369.         xcode = item->fail.code; \
  370.         xtext = item->fail.text; \
  371.         state.count = item->fail.count; \
  372.         state.level = item->fail.level; \
  373.         state.point--; \
  374.     } \
  375.     while (item->fail.text == NULL); \
  376. }
  377.  
  378. enum regexp_compiled_ops /* opcodes for compiled regexp */
  379. {
  380.     Cend,              /* end of pattern reached */
  381.     Cbol,              /* beginning of line */
  382.     Ceol,              /* end of line */
  383.     Cset,              /* character set.  Followed by 32 bytes of set. */
  384.     Cexact,              /* followed by a byte to match */
  385.     Canychar,          /* matches any character except newline */
  386.     Cstart_memory,          /* set register start addr (followed by reg number) */
  387.     Cend_memory,          /* set register end addr (followed by reg number) */
  388.     Cmatch_memory,          /* match a duplicate of reg contents (regnum follows)*/
  389.     Cjump,              /* followed by two bytes (lsb,msb) of displacement. */
  390.     Cstar_jump,          /* will change to jump/update_failure_jump at runtime */
  391.     Cfailure_jump,          /* jump to addr on failure */
  392.     Cupdate_failure_jump, /* update topmost failure point and jump */
  393.     Cdummy_failure_jump,  /* push a dummy failure point and jump */
  394.     Cbegbuf,          /* match at beginning of buffer */
  395.     Cendbuf,          /* match at end of buffer */
  396.     Cwordbeg,          /* match at beginning of word */
  397.     Cwordend,          /* match at end of word */
  398.     Cwordbound,          /* match if at word boundary */
  399.     Cnotwordbound,        /* match if not at word boundary */
  400.     Csyntaxspec,          /* matches syntax code (1 byte follows) */
  401.     Cnotsyntaxspec,       /* matches if syntax code does not match (1 byte follows) */
  402.     Crepeat1
  403. };
  404.  
  405. enum regexp_syntax_op    /* syntax codes for plain and quoted characters */
  406. {
  407.     Rend,          /* special code for end of regexp */
  408.     Rnormal,      /* normal character */
  409.     Ranychar,      /* any character except newline */
  410.     Rquote,          /* the quote character */
  411.     Rbol,          /* match beginning of line */
  412.     Reol,          /* match end of line */
  413.     Roptional,      /* match preceding expression optionally */
  414.     Rstar,          /* match preceding expr zero or more times */
  415.     Rplus,          /* match preceding expr one or more times */
  416.     Ror,          /* match either of alternatives */
  417.     Ropenpar,      /* opening parenthesis */
  418.     Rclosepar,      /* closing parenthesis */
  419.     Rmemory,      /* match memory register */
  420.     Rextended_memory, /* \vnn to match registers 10-99 */
  421.     Ropenset,      /* open set.  Internal syntax hard-coded below. */
  422.     /* the following are gnu extensions to "normal" regexp syntax */
  423.     Rbegbuf,      /* beginning of buffer */
  424.     Rendbuf,      /* end of buffer */
  425.     Rwordchar,      /* word character */
  426.     Rnotwordchar,      /* not word character */
  427.     Rwordbeg,      /* beginning of word */
  428.     Rwordend,      /* end of word */
  429.     Rwordbound,      /* word bound */
  430.     Rnotwordbound,      /* not word bound */
  431.     Rnum_ops
  432. };
  433.  
  434. static int re_compile_initialized = 0;
  435. static int regexp_syntax = 0;
  436. int re_syntax = 0; /* Exported copy of regexp_syntax */
  437. static unsigned char regexp_plain_ops[256];
  438. static unsigned char regexp_quoted_ops[256];
  439. static unsigned char regexp_precedences[Rnum_ops];
  440. static int regexp_context_indep_ops;
  441. static int regexp_ansi_sequences;
  442.  
  443. #define NUM_LEVELS  5    /* number of precedence levels in use */
  444. #define MAX_NESTING 100  /* max nesting level of operators */
  445.  
  446. #define SYNTAX(ch) re_syntax_table[(unsigned char)(ch)]
  447.  
  448. unsigned char re_syntax_table[256];
  449.  
  450. void re_compile_initialize()
  451. {
  452.     int a;
  453.   
  454.     static int syntax_table_inited = 0;
  455.  
  456.     if (!syntax_table_inited)
  457.     {
  458.         syntax_table_inited = 1;
  459.         memset(re_syntax_table, 0, 256);
  460.         for (a = 'a'; a <= 'z'; a++)
  461.             re_syntax_table[a] = Sword;
  462.         for (a = 'A'; a <= 'Z'; a++)
  463.             re_syntax_table[a] = Sword;
  464.         for (a = '0'; a <= '9'; a++)
  465.             re_syntax_table[a] = Sword | Sdigit | Shexdigit;
  466.         for (a = '0'; a <= '7'; a++)
  467.             re_syntax_table[a] |= Soctaldigit;
  468.         for (a = 'A'; a <= 'F'; a++)
  469.             re_syntax_table[a] |= Shexdigit;
  470.         for (a = 'a'; a <= 'f'; a++)
  471.             re_syntax_table[a] |= Shexdigit;
  472.         re_syntax_table['_'] = Sword;
  473.         for (a = 9; a <= 13; a++)
  474.             re_syntax_table[a] = Swhitespace;
  475.         re_syntax_table[' '] = Swhitespace;
  476.     }
  477.     re_compile_initialized = 1;
  478.     for (a = 0; a < 256; a++)
  479.     {
  480.         regexp_plain_ops[a] = Rnormal;
  481.         regexp_quoted_ops[a] = Rnormal;
  482.     }
  483.     for (a = '0'; a <= '9'; a++)
  484.         regexp_quoted_ops[a] = Rmemory;
  485.     regexp_plain_ops['\134'] = Rquote;
  486.     if (regexp_syntax & RE_NO_BK_PARENS)
  487.     {
  488.         regexp_plain_ops['('] = Ropenpar;
  489.         regexp_plain_ops[')'] = Rclosepar;
  490.     }
  491.     else
  492.     {
  493.         regexp_quoted_ops['('] = Ropenpar;
  494.         regexp_quoted_ops[')'] = Rclosepar;
  495.     }
  496.     if (regexp_syntax & RE_NO_BK_VBAR)
  497.         regexp_plain_ops['\174'] = Ror;
  498.     else
  499.         regexp_quoted_ops['\174'] = Ror;
  500.     regexp_plain_ops['*'] = Rstar;
  501.     if (regexp_syntax & RE_BK_PLUS_QM)
  502.     {
  503.         regexp_quoted_ops['+'] = Rplus;
  504.         regexp_quoted_ops['?'] = Roptional;
  505.     }
  506.     else
  507.     {
  508.         regexp_plain_ops['+'] = Rplus;
  509.         regexp_plain_ops['?'] = Roptional;
  510.     }
  511.     if (regexp_syntax & RE_NEWLINE_OR)
  512.         regexp_plain_ops['\n'] = Ror;
  513.     regexp_plain_ops['\133'] = Ropenset;
  514.     regexp_plain_ops['\136'] = Rbol;
  515.     regexp_plain_ops['$'] = Reol;
  516.     regexp_plain_ops['.'] = Ranychar;
  517.     if (!(regexp_syntax & RE_NO_GNU_EXTENSIONS))
  518.     {
  519.         regexp_quoted_ops['w'] = Rwordchar;
  520.         regexp_quoted_ops['W'] = Rnotwordchar;
  521.         regexp_quoted_ops['<'] = Rwordbeg;
  522.         regexp_quoted_ops['>'] = Rwordend;
  523.         regexp_quoted_ops['b'] = Rwordbound;
  524.         regexp_quoted_ops['B'] = Rnotwordbound;
  525.         regexp_quoted_ops['`'] = Rbegbuf;
  526.         regexp_quoted_ops['\''] = Rendbuf;
  527.     }
  528.     if (regexp_syntax & RE_ANSI_HEX)
  529.         regexp_quoted_ops['v'] = Rextended_memory;
  530.     for (a = 0; a < Rnum_ops; a++)
  531.         regexp_precedences[a] = 4;
  532.     if (regexp_syntax & RE_TIGHT_VBAR)
  533.     {
  534.         regexp_precedences[Ror] = 3;
  535.         regexp_precedences[Rbol] = 2;
  536.         regexp_precedences[Reol] = 2;
  537.     }
  538.     else
  539.     {
  540.         regexp_precedences[Ror] = 2;
  541.         regexp_precedences[Rbol] = 3;
  542.         regexp_precedences[Reol] = 3;
  543.     }
  544.     regexp_precedences[Rclosepar] = 1;
  545.     regexp_precedences[Rend] = 0;
  546.     regexp_context_indep_ops = (regexp_syntax & RE_CONTEXT_INDEP_OPS) != 0;
  547.     regexp_ansi_sequences = (regexp_syntax & RE_ANSI_HEX) != 0;
  548. }
  549.  
  550. int re_set_syntax(syntax)
  551.     int syntax;
  552. {
  553.     int ret;
  554.     
  555.     ret = regexp_syntax;
  556.     regexp_syntax = syntax;
  557.     re_syntax = syntax; /* Exported copy */
  558.     re_compile_initialize();
  559.     return ret;
  560. }
  561.  
  562. static int hex_char_to_decimal(ch)
  563.     int ch;
  564. {
  565.     if (ch >= '0' && ch <= '9')
  566.         return ch - '0';
  567.     if (ch >= 'a' && ch <= 'f')
  568.         return ch - 'a' + 10;
  569.     if (ch >= 'A' && ch <= 'F')
  570.         return ch - 'A' + 10;
  571.     return 16;
  572. }
  573.  
  574. static void re_compile_fastmap_aux(code,
  575.                    pos,
  576.                    visited,
  577.                    can_be_null,
  578.                    fastmap)
  579.     unsigned char *code;
  580.     int pos;
  581.     unsigned char *visited;
  582.     unsigned char *can_be_null;
  583.     unsigned char *fastmap;
  584. {
  585.     int a;
  586.     int b;
  587.     int syntaxcode;
  588.     
  589.     if (visited[pos])
  590.         return;  /* we have already been here */
  591.     visited[pos] = 1;
  592.     for (;;)
  593.         switch (code[pos++]) {
  594.         case Cend:
  595.             {
  596.                 *can_be_null = 1;
  597.                 return;
  598.             }
  599.         case Cbol:
  600.         case Cbegbuf:
  601.         case Cendbuf:
  602.         case Cwordbeg:
  603.         case Cwordend:
  604.         case Cwordbound:
  605.         case Cnotwordbound:
  606.         {
  607.             for (a = 0; a < 256; a++)
  608.                 fastmap[a] = 1;
  609.             break;
  610.         }
  611.         case Csyntaxspec:
  612.         {
  613.             syntaxcode = code[pos++];
  614.             for (a = 0; a < 256; a++)
  615.                 if (SYNTAX(a) & syntaxcode) 
  616.                     fastmap[a] = 1;
  617.             return;
  618.         }
  619.         case Cnotsyntaxspec:
  620.         {
  621.             syntaxcode = code[pos++];
  622.             for (a = 0; a < 256; a++)
  623.                 if (!(SYNTAX(a) & syntaxcode) )
  624.                     fastmap[a] = 1;
  625.             return;
  626.         }
  627.         case Ceol:
  628.         {
  629.             fastmap['\n'] = 1;
  630.             if (*can_be_null == 0)
  631.                 *can_be_null = 2; /* can match null, but only at end of buffer*/
  632.             return;
  633.         }
  634.         case Cset:
  635.         {
  636.             for (a = 0; a < 256/8; a++)
  637.                 if (code[pos + a] != 0)
  638.                     for (b = 0; b < 8; b++)
  639.                         if (code[pos + a] & (1 << b))
  640.                             fastmap[(a << 3) + b] = 1;
  641.             pos += 256/8;
  642.             return;
  643.         }
  644.         case Cexact:
  645.         {
  646.             fastmap[(unsigned char)code[pos]] = 1;
  647.             return;
  648.         }
  649.         case Canychar:
  650.         {
  651.             for (a = 0; a < 256; a++)
  652.                 if (a != '\n')
  653.                     fastmap[a] = 1;
  654.             return;
  655.         }
  656.         case Cstart_memory:
  657.         case Cend_memory:
  658.         {
  659.             pos++;
  660.             break;
  661.         }
  662.         case Cmatch_memory:
  663.         {
  664.             for (a = 0; a < 256; a++)
  665.                 fastmap[a] = 1;
  666.             *can_be_null = 1;
  667.             return;
  668.         }
  669.         case Cjump:
  670.         case Cdummy_failure_jump:
  671.         case Cupdate_failure_jump:
  672.         case Cstar_jump:
  673.         {
  674.             a = (unsigned char)code[pos++];
  675.             a |= (unsigned char)code[pos++] << 8;
  676.             pos += (int)SHORT(a);
  677.             if (visited[pos])
  678.             {
  679.                 /* argh... the regexp contains empty loops.  This is not
  680.                    good, as this may cause a failure stack overflow when
  681.                    matching.  Oh well. */
  682.                 /* this path leads nowhere; pursue other paths. */
  683.                 return;
  684.             }
  685.             visited[pos] = 1;
  686.             break;
  687.         }
  688.         case Cfailure_jump:
  689.         {
  690.             a = (unsigned char)code[pos++];
  691.             a |= (unsigned char)code[pos++] << 8;
  692.             a = pos + (int)SHORT(a);
  693.             re_compile_fastmap_aux(code, a, visited, can_be_null, fastmap);
  694.             break;
  695.         }
  696.         case Crepeat1:
  697.         {
  698.             pos += 2;
  699.             break;
  700.         }
  701.         default:
  702.         {
  703.                 PyErr_SetString(PyExc_SystemError, "Unknown regex opcode: memory corrupted?");
  704.                 return;
  705.             /*NOTREACHED*/
  706.         }
  707.         }
  708. }
  709.  
  710. static int re_do_compile_fastmap(buffer,
  711.                  used,
  712.                  pos,
  713.                  can_be_null,
  714.                  fastmap)
  715.     unsigned char *buffer;
  716.     int used;
  717.     int pos;
  718.     unsigned char *can_be_null;
  719.     unsigned char *fastmap;
  720. {
  721.     unsigned char small_visited[512], *visited;
  722.    
  723.     if (used <= sizeof(small_visited))
  724.         visited = small_visited;
  725.     else
  726.     {
  727.         visited = malloc(used);
  728.         if (!visited)
  729.             return 0;
  730.     }
  731.     *can_be_null = 0;
  732.     memset(fastmap, 0, 256);
  733.     memset(visited, 0, used);
  734.     re_compile_fastmap_aux(buffer, pos, visited, can_be_null, fastmap);
  735.     if (visited != small_visited)
  736.         free(visited);
  737.     return 1;
  738. }
  739.  
  740. void re_compile_fastmap(bufp)
  741.     regexp_t bufp;
  742. {
  743.     if (!bufp->fastmap || bufp->fastmap_accurate)
  744.         return;
  745.     assert(bufp->used > 0);
  746.     if (!re_do_compile_fastmap(bufp->buffer,
  747.                    bufp->used,
  748.                    0,
  749.                    &bufp->can_be_null,
  750.                    bufp->fastmap))
  751.         return;
  752.     if (PyErr_Occurred()) return;
  753.     if (bufp->buffer[0] == Cbol)
  754.         bufp->anchor = 1;   /* begline */
  755.     else
  756.         if (bufp->buffer[0] == Cbegbuf)
  757.             bufp->anchor = 2; /* begbuf */
  758.         else
  759.             bufp->anchor = 0; /* none */
  760.     bufp->fastmap_accurate = 1;
  761. }
  762.  
  763. /* 
  764.  * star is coded as:
  765.  * 1: failure_jump 2
  766.  *    ... code for operand of star
  767.  *    star_jump 1
  768.  * 2: ... code after star
  769.  *
  770.  * We change the star_jump to update_failure_jump if we can determine
  771.  * that it is safe to do so; otherwise we change it to an ordinary
  772.  * jump.
  773.  *
  774.  * plus is coded as
  775.  *
  776.  *    jump 2
  777.  * 1: failure_jump 3
  778.  * 2: ... code for operand of plus
  779.  *    star_jump 1
  780.  * 3: ... code after plus
  781.  *
  782.  * For star_jump considerations this is processed identically to star.
  783.  *
  784.  */
  785.  
  786. static int re_optimize_star_jump(bufp, code)
  787.     regexp_t bufp;
  788.     unsigned char *code;
  789. {
  790.     unsigned char map[256];
  791.     unsigned char can_be_null;
  792.     unsigned char *p1;
  793.     unsigned char *p2;
  794.     unsigned char ch;
  795.     int a;
  796.     int b;
  797.     int num_instructions = 0;
  798.  
  799.     a = (unsigned char)*code++;
  800.     a |= (unsigned char)*code++ << 8;
  801.     a = (int)SHORT(a);
  802.     
  803.     p1 = code + a + 3; /* skip the failure_jump */
  804.     /* Check that the jump is within the pattern */
  805.     if (p1<bufp->buffer || bufp->buffer+bufp->used<p1)
  806.       {
  807.         PyErr_SetString(PyExc_SystemError, "Regex VM jump out of bounds (failure_jump opt)");
  808.         return 0;
  809.       }
  810.     
  811.     assert(p1[-3] == Cfailure_jump);
  812.     p2 = code;
  813.     /* p1 points inside loop, p2 points to after loop */
  814.     if (!re_do_compile_fastmap(bufp->buffer, bufp->used,
  815.                    (int)(p2 - bufp->buffer),
  816.                    &can_be_null, map))
  817.         goto make_normal_jump;
  818.     
  819.     /* If we might introduce a new update point inside the
  820.      * loop, we can't optimize because then update_jump would
  821.      * update a wrong failure point.  Thus we have to be
  822.      * quite careful here.
  823.      */
  824.     
  825.     /* loop until we find something that consumes a character */
  826.   loop_p1:
  827.     num_instructions++;
  828.     switch (*p1++)
  829.     {
  830.     case Cbol:
  831.     case Ceol:
  832.     case Cbegbuf:
  833.     case Cendbuf:
  834.     case Cwordbeg:
  835.     case Cwordend:
  836.     case Cwordbound:
  837.     case Cnotwordbound:
  838.     {
  839.         goto loop_p1;
  840.     }
  841.     case Cstart_memory:
  842.     case Cend_memory:
  843.     {
  844.         p1++;
  845.         goto loop_p1;
  846.     }
  847.     case Cexact:
  848.     {
  849.         ch = (unsigned char)*p1++;
  850.         if (map[(int)ch])
  851.             goto make_normal_jump;
  852.         break;
  853.     }
  854.     case Canychar:
  855.     {
  856.         for (b = 0; b < 256; b++)
  857.             if (b != '\n' && map[b])
  858.                 goto make_normal_jump;
  859.         break;
  860.     }
  861.     case Cset:
  862.     {
  863.         for (b = 0; b < 256; b++)
  864.             if ((p1[b >> 3] & (1 << (b & 7))) && map[b])
  865.                 goto make_normal_jump;
  866.         p1 += 256/8;
  867.         break;
  868.     }
  869.     default:
  870.     {
  871.         goto make_normal_jump;
  872.     }
  873.     }
  874.     /* now we know that we can't backtrack. */
  875.     while (p1 != p2 - 3)
  876.     {
  877.         num_instructions++;
  878.         switch (*p1++)
  879.         {
  880.         case Cend:
  881.         {
  882.             return 0;
  883.         }
  884.         case Cbol:
  885.         case Ceol:
  886.         case Canychar:
  887.         case Cbegbuf:
  888.         case Cendbuf:
  889.         case Cwordbeg:
  890.         case Cwordend:
  891.         case Cwordbound:
  892.         case Cnotwordbound:
  893.         {
  894.             break;
  895.         }
  896.         case Cset:
  897.         {
  898.             p1 += 256/8;
  899.             break;
  900.         }
  901.         case Cexact:
  902.         case Cstart_memory:
  903.         case Cend_memory:
  904.         case Cmatch_memory:
  905.         case Csyntaxspec:
  906.         case Cnotsyntaxspec:
  907.         {
  908.             p1++;
  909.             break;
  910.         }
  911.         case Cjump:
  912.         case Cstar_jump:
  913.         case Cfailure_jump:
  914.         case Cupdate_failure_jump:
  915.         case Cdummy_failure_jump:
  916.         {
  917.             goto make_normal_jump;
  918.         }
  919.         default:
  920.         {
  921.             return 0;
  922.         }
  923.         }
  924.     }
  925.     
  926.     /* make_update_jump: */
  927.     code -= 3;
  928.     a += 3;  /* jump to after the Cfailure_jump */
  929.     code[0] = Cupdate_failure_jump;
  930.     code[1] = a & 0xff;
  931.     code[2] = a >> 8;
  932.     if (num_instructions > 1)
  933.         return 1;
  934.     assert(num_instructions == 1);
  935.     /* if the only instruction matches a single character, we can do
  936.      * better */
  937.     p1 = code + 3 + a;   /* start of sole instruction */
  938.     if (*p1 == Cset || *p1 == Cexact || *p1 == Canychar ||
  939.         *p1 == Csyntaxspec || *p1 == Cnotsyntaxspec)
  940.         code[0] = Crepeat1;
  941.     return 1;
  942.     
  943.   make_normal_jump:
  944.     code -= 3;
  945.     *code = Cjump;
  946.     return 1;
  947. }
  948.  
  949. static int re_optimize(bufp)
  950.     regexp_t bufp;
  951. {
  952.     unsigned char *code;
  953.     
  954.     code = bufp->buffer;
  955.     
  956.     while(1)
  957.     {
  958.         switch (*code++)
  959.         {
  960.         case Cend:
  961.         {
  962.             return 1;
  963.         }
  964.         case Canychar:
  965.         case Cbol:
  966.         case Ceol:
  967.         case Cbegbuf:
  968.         case Cendbuf:
  969.         case Cwordbeg:
  970.         case Cwordend:
  971.         case Cwordbound:
  972.         case Cnotwordbound:
  973.         {
  974.             break;
  975.         }
  976.         case Cset:
  977.         {
  978.             code += 256/8;
  979.             break;
  980.         }
  981.         case Cexact:
  982.         case Cstart_memory:
  983.         case Cend_memory:
  984.         case Cmatch_memory:
  985.         case Csyntaxspec:
  986.         case Cnotsyntaxspec:
  987.         {
  988.             code++;
  989.             break;
  990.         }
  991.         case Cstar_jump:
  992.         {
  993.             if (!re_optimize_star_jump(bufp, code))
  994.             {
  995.                 return 0;
  996.             }
  997.             /* fall through */
  998.         }
  999.         case Cupdate_failure_jump:
  1000.         case Cjump:
  1001.         case Cdummy_failure_jump:
  1002.         case Cfailure_jump:
  1003.         case Crepeat1:
  1004.         {
  1005.             code += 2;
  1006.             break;
  1007.         }
  1008.         default:
  1009.         {
  1010.             return 0;
  1011.         }
  1012.         }
  1013.     }
  1014. }
  1015.  
  1016. #define NEXTCHAR(var) \
  1017. { \
  1018.     if (pos >= size) \
  1019.         goto ends_prematurely; \
  1020.     (var) = regex[pos]; \
  1021.     pos++; \
  1022. }
  1023.  
  1024. #define ALLOC(amount) \
  1025. { \
  1026.       if (pattern_offset+(amount) > alloc) \
  1027.       { \
  1028.           alloc += 256 + (amount); \
  1029.           pattern = realloc(pattern, alloc); \
  1030.           if (!pattern) \
  1031.               goto out_of_memory; \
  1032.       } \
  1033. }
  1034.  
  1035. #define STORE(ch) pattern[pattern_offset++] = (ch)
  1036.  
  1037. #define CURRENT_LEVEL_START (starts[starts_base + current_level])
  1038.  
  1039. #define SET_LEVEL_START starts[starts_base + current_level] = pattern_offset
  1040.  
  1041. #define PUSH_LEVEL_STARTS \
  1042. if (starts_base < (MAX_NESTING-1)*NUM_LEVELS) \
  1043.     starts_base += NUM_LEVELS; \
  1044. else \
  1045.     goto too_complex \
  1046.  
  1047. #define POP_LEVEL_STARTS starts_base -= NUM_LEVELS
  1048.  
  1049. #define PUT_ADDR(offset,addr) \
  1050. { \
  1051.     int disp = (addr) - (offset) - 2; \
  1052.     pattern[(offset)] = disp & 0xff; \
  1053.     pattern[(offset)+1] = (disp>>8) & 0xff; \
  1054. }
  1055.  
  1056. #define INSERT_JUMP(pos,type,addr) \
  1057. { \
  1058.     int a, p = (pos), t = (type), ad = (addr); \
  1059.     for (a = pattern_offset - 1; a >= p; a--) \
  1060.         pattern[a + 3] = pattern[a]; \
  1061.     pattern[p] = t; \
  1062.     PUT_ADDR(p+1,ad); \
  1063.     pattern_offset += 3; \
  1064. }
  1065.  
  1066. #define SETBIT(buf,offset,bit) (buf)[(offset)+(bit)/8] |= (1<<((bit) & 7))
  1067.  
  1068. #define SET_FIELDS \
  1069. { \
  1070.     bufp->allocated = alloc; \
  1071.     bufp->buffer = pattern; \
  1072.     bufp->used = pattern_offset; \
  1073. }
  1074.     
  1075. #define GETHEX(var) \
  1076. { \
  1077.     unsigned char gethex_ch, gethex_value; \
  1078.     NEXTCHAR(gethex_ch); \
  1079.     gethex_value = hex_char_to_decimal(gethex_ch); \
  1080.     if (gethex_value == 16) \
  1081.         goto hex_error; \
  1082.     NEXTCHAR(gethex_ch); \
  1083.     gethex_ch = hex_char_to_decimal(gethex_ch); \
  1084.     if (gethex_ch == 16) \
  1085.         goto hex_error; \
  1086.     (var) = gethex_value * 16 + gethex_ch; \
  1087. }
  1088.  
  1089. #define ANSI_TRANSLATE(ch) \
  1090. { \
  1091.     switch (ch) \
  1092.     { \
  1093.     case 'a': \
  1094.     case 'A': \
  1095.     { \
  1096.         ch = 7; /* audible bell */ \
  1097.         break; \
  1098.     } \
  1099.     case 'b': \
  1100.     case 'B': \
  1101.     { \
  1102.         ch = 8; /* backspace */ \
  1103.         break; \
  1104.     } \
  1105.     case 'f': \
  1106.     case 'F': \
  1107.     { \
  1108.         ch = 12; /* form feed */ \
  1109.         break; \
  1110.     } \
  1111.     case 'n': \
  1112.     case 'N': \
  1113.     { \
  1114.         ch = 10; /* line feed */ \
  1115.         break; \
  1116.     } \
  1117.     case 'r': \
  1118.     case 'R': \
  1119.     { \
  1120.         ch = 13; /* carriage return */ \
  1121.         break; \
  1122.     } \
  1123.     case 't': \
  1124.     case 'T': \
  1125.     { \
  1126.           ch = 9; /* tab */ \
  1127.           break; \
  1128.     } \
  1129.     case 'v': \
  1130.     case 'V': \
  1131.     { \
  1132.         ch = 11; /* vertical tab */ \
  1133.         break; \
  1134.     } \
  1135.     case 'x': /* hex code */ \
  1136.     case 'X': \
  1137.     { \
  1138.         GETHEX(ch); \
  1139.         break; \
  1140.     } \
  1141.     default: \
  1142.     { \
  1143.         /* other characters passed through */ \
  1144.         if (translate) \
  1145.             ch = translate[(unsigned char)ch]; \
  1146.         break; \
  1147.     } \
  1148.     } \
  1149. }
  1150.  
  1151. char *re_compile_pattern(regex, size, bufp)
  1152.     unsigned char *regex;
  1153.     int size;
  1154.     regexp_t bufp;
  1155. {
  1156.     int a;
  1157.     int pos;
  1158.     int op;
  1159.     int current_level;
  1160.     int level;
  1161.     int opcode;
  1162.     int pattern_offset = 0, alloc;
  1163.     int starts[NUM_LEVELS * MAX_NESTING];
  1164.     int starts_base;
  1165.     int future_jumps[MAX_NESTING];
  1166.     int num_jumps;
  1167.     unsigned char ch = '\0';
  1168.     unsigned char *pattern;
  1169.     unsigned char *translate;
  1170.     int next_register;
  1171.     int paren_depth;
  1172.     int num_open_registers;
  1173.     int open_registers[RE_NREGS];
  1174.     int beginning_context;
  1175.     
  1176.     if (!re_compile_initialized)
  1177.         re_compile_initialize();
  1178.     bufp->used = 0;
  1179.     bufp->fastmap_accurate = 0;
  1180.     bufp->uses_registers = 1;
  1181.     bufp->num_registers = 1;
  1182.     translate = bufp->translate;
  1183.     pattern = bufp->buffer;
  1184.     alloc = bufp->allocated;
  1185.     if (alloc == 0 || pattern == NULL)
  1186.     {
  1187.         alloc = 256;
  1188.         pattern = malloc(alloc);
  1189.         if (!pattern)
  1190.             goto out_of_memory;
  1191.     }
  1192.     pattern_offset = 0;
  1193.     starts_base = 0;
  1194.     num_jumps = 0;
  1195.     current_level = 0;
  1196.     SET_LEVEL_START;
  1197.     num_open_registers = 0;
  1198.     next_register = 1;
  1199.     paren_depth = 0;
  1200.     beginning_context = 1;
  1201.     op = -1;
  1202.     /* we use Rend dummy to ensure that pending jumps are updated
  1203.        (due to low priority of Rend) before exiting the loop. */
  1204.     pos = 0;
  1205.     while (op != Rend)
  1206.     {
  1207.         if (pos >= size)
  1208.             op = Rend;
  1209.         else
  1210.         {
  1211.             NEXTCHAR(ch);
  1212.             if (translate)
  1213.                 ch = translate[(unsigned char)ch];
  1214.             op = regexp_plain_ops[(unsigned char)ch];
  1215.             if (op == Rquote)
  1216.             {
  1217.                 NEXTCHAR(ch);
  1218.                 op = regexp_quoted_ops[(unsigned char)ch];
  1219.                 if (op == Rnormal && regexp_ansi_sequences)
  1220.                     ANSI_TRANSLATE(ch);
  1221.             }
  1222.         }
  1223.         level = regexp_precedences[op];
  1224.         /* printf("ch='%c' op=%d level=%d current_level=%d
  1225.            curlevstart=%d\n", ch, op, level, current_level,
  1226.            CURRENT_LEVEL_START); */
  1227.         if (level > current_level)
  1228.         {
  1229.             for (current_level++; current_level < level; current_level++)
  1230.                 SET_LEVEL_START;
  1231.             SET_LEVEL_START;
  1232.         }
  1233.         else
  1234.             if (level < current_level)
  1235.             {
  1236.                 current_level = level;
  1237.                 for (;num_jumps > 0 &&
  1238.                          future_jumps[num_jumps-1] >= CURRENT_LEVEL_START;
  1239.                      num_jumps--)
  1240.                     PUT_ADDR(future_jumps[num_jumps-1], pattern_offset);
  1241.             }
  1242.         switch (op)
  1243.         {
  1244.         case Rend:
  1245.         {
  1246.             break;
  1247.         }
  1248.         case Rnormal:
  1249.         {
  1250.           normal_char:
  1251.             opcode = Cexact;
  1252.           store_opcode_and_arg: /* opcode & ch must be set */
  1253.             SET_LEVEL_START;
  1254.             ALLOC(2);
  1255.             STORE(opcode);
  1256.             STORE(ch);
  1257.             break;
  1258.         }
  1259.         case Ranychar:
  1260.         {
  1261.             opcode = Canychar;
  1262.           store_opcode:
  1263.             SET_LEVEL_START;
  1264.             ALLOC(1);
  1265.             STORE(opcode);
  1266.             break;
  1267.         }
  1268.         case Rquote:
  1269.         {
  1270.             abort();
  1271.             /*NOTREACHED*/
  1272.         }
  1273.         case Rbol:
  1274.         {
  1275.             if (!beginning_context) {
  1276.                 if (regexp_context_indep_ops)
  1277.                     goto op_error;
  1278.                 else
  1279.                     goto normal_char;
  1280.             }
  1281.             opcode = Cbol;
  1282.             goto store_opcode;
  1283.         }
  1284.         case Reol:
  1285.         {
  1286.             if (!((pos >= size) ||
  1287.                   ((regexp_syntax & RE_NO_BK_VBAR) ?
  1288.                    (regex[pos] == '\174') :
  1289.                    (pos+1 < size && regex[pos] == '\134' &&
  1290.                 regex[pos+1] == '\174')) ||
  1291.                   ((regexp_syntax & RE_NO_BK_PARENS)?
  1292.                    (regex[pos] == ')'):
  1293.                    (pos+1 < size && regex[pos] == '\134' &&
  1294.                 regex[pos+1] == ')')))) {
  1295.                 if (regexp_context_indep_ops)
  1296.                     goto op_error;
  1297.                 else
  1298.                     goto normal_char;
  1299.             }
  1300.             opcode = Ceol;
  1301.             goto store_opcode;
  1302.             /* NOTREACHED */
  1303.             break;
  1304.         }
  1305.         case Roptional:
  1306.         {
  1307.             if (beginning_context) {
  1308.                 if (regexp_context_indep_ops)
  1309.                     goto op_error;
  1310.                 else
  1311.                     goto normal_char;
  1312.             }
  1313.             if (CURRENT_LEVEL_START == pattern_offset)
  1314.                 break; /* ignore empty patterns for ? */
  1315.             ALLOC(3);
  1316.             INSERT_JUMP(CURRENT_LEVEL_START, Cfailure_jump,
  1317.                     pattern_offset + 3);
  1318.             break;
  1319.         }
  1320.         case Rstar:
  1321.         case Rplus:
  1322.         {
  1323.             if (beginning_context) {
  1324.                 if (regexp_context_indep_ops)
  1325.                     goto op_error;
  1326.                 else
  1327.                     goto normal_char;
  1328.             }
  1329.             if (CURRENT_LEVEL_START == pattern_offset)
  1330.                 break; /* ignore empty patterns for + and * */
  1331.             ALLOC(9);
  1332.             INSERT_JUMP(CURRENT_LEVEL_START, Cfailure_jump,
  1333.                     pattern_offset + 6);
  1334.             INSERT_JUMP(pattern_offset, Cstar_jump, CURRENT_LEVEL_START);
  1335.             if (op == Rplus)  /* jump over initial failure_jump */
  1336.                 INSERT_JUMP(CURRENT_LEVEL_START, Cdummy_failure_jump,
  1337.                         CURRENT_LEVEL_START + 6);
  1338.             break;
  1339.         }
  1340.         case Ror:
  1341.         {
  1342.             ALLOC(6);
  1343.             INSERT_JUMP(CURRENT_LEVEL_START, Cfailure_jump,
  1344.                     pattern_offset + 6);
  1345.             if (num_jumps >= MAX_NESTING)
  1346.                 goto too_complex;
  1347.             STORE(Cjump);
  1348.             future_jumps[num_jumps++] = pattern_offset;
  1349.             STORE(0);
  1350.             STORE(0);
  1351.             SET_LEVEL_START;
  1352.             break;
  1353.         }
  1354.         case Ropenpar:
  1355.         {
  1356.             SET_LEVEL_START;
  1357.             if (next_register < RE_NREGS)
  1358.             {
  1359.                 bufp->uses_registers = 1;
  1360.                 ALLOC(2);
  1361.                 STORE(Cstart_memory);
  1362.                 STORE(next_register);
  1363.                 open_registers[num_open_registers++] = next_register;
  1364.                 bufp->num_registers++;
  1365.                 next_register++;
  1366.             }
  1367.             paren_depth++;
  1368.             PUSH_LEVEL_STARTS;
  1369.             current_level = 0;
  1370.             SET_LEVEL_START;
  1371.             break;
  1372.         }
  1373.         case Rclosepar:
  1374.         {
  1375.             if (paren_depth <= 0)
  1376.                 goto parenthesis_error;
  1377.             POP_LEVEL_STARTS;
  1378.             current_level = regexp_precedences[Ropenpar];
  1379.             paren_depth--;
  1380.             if (paren_depth < num_open_registers)
  1381.             {
  1382.                 bufp->uses_registers = 1;
  1383.                 ALLOC(2);
  1384.                 STORE(Cend_memory);
  1385.                 num_open_registers--;
  1386.                 STORE(open_registers[num_open_registers]);
  1387.             }
  1388.             break;
  1389.         }
  1390.         case Rmemory:
  1391.         {
  1392.             if (ch == '0')
  1393.                 goto bad_match_register;
  1394.             assert(ch >= '0' && ch <= '9');
  1395.             bufp->uses_registers = 1;
  1396.             opcode = Cmatch_memory;
  1397.             ch -= '0';
  1398.             goto store_opcode_and_arg;
  1399.         }
  1400.         case Rextended_memory:
  1401.         {
  1402.             NEXTCHAR(ch);
  1403.             if (ch < '0' || ch > '9')
  1404.                 goto bad_match_register;
  1405.             NEXTCHAR(a);
  1406.             if (a < '0' || a > '9')
  1407.                 goto bad_match_register;
  1408.             ch = 10 * (a - '0') + ch - '0';
  1409.             if (ch <= 0 || ch >= RE_NREGS)
  1410.                 goto bad_match_register;
  1411.             bufp->uses_registers = 1;
  1412.             opcode = Cmatch_memory;
  1413.             goto store_opcode_and_arg;
  1414.         }
  1415.         case Ropenset:
  1416.         {
  1417.             int complement;
  1418.             int prev;
  1419.             int offset;
  1420.             int range;
  1421.             int firstchar;
  1422.         
  1423.             SET_LEVEL_START;
  1424.             ALLOC(1+256/8);
  1425.             STORE(Cset);
  1426.             offset = pattern_offset;
  1427.             for (a = 0; a < 256/8; a++)
  1428.                 STORE(0);
  1429.             NEXTCHAR(ch);
  1430.             if (translate)
  1431.                 ch = translate[(unsigned char)ch];
  1432.             if (ch == '\136')
  1433.             {
  1434.                 complement = 1;
  1435.                 NEXTCHAR(ch);
  1436.                 if (translate)
  1437.                     ch = translate[(unsigned char)ch];
  1438.             }
  1439.             else
  1440.                 complement = 0;
  1441.             prev = -1;
  1442.             range = 0;
  1443.             firstchar = 1;
  1444.             while (ch != '\135' || firstchar)
  1445.             {
  1446.                 firstchar = 0;
  1447.                 if (regexp_ansi_sequences && ch == '\134')
  1448.                 {
  1449.                     NEXTCHAR(ch);
  1450.                     ANSI_TRANSLATE(ch);
  1451.                 }
  1452.                 if (range)
  1453.                 {
  1454.                     for (a = prev; a <= (int)ch; a++)
  1455.                         SETBIT(pattern, offset, a);
  1456.                     prev = -1;
  1457.                     range = 0;
  1458.                 }
  1459.                 else
  1460.                     if (prev != -1 && ch == '-')
  1461.                         range = 1;
  1462.                     else
  1463.                     {
  1464.                         SETBIT(pattern, offset, ch);
  1465.                         prev = ch;
  1466.                     }
  1467.                 NEXTCHAR(ch);
  1468.                 if (translate)
  1469.                     ch = translate[(unsigned char)ch];
  1470.             }
  1471.             if (range)
  1472.                 SETBIT(pattern, offset, '-');
  1473.             if (complement)
  1474.             {
  1475.                 for (a = 0; a < 256/8; a++)
  1476.                     pattern[offset+a] ^= 0xff;
  1477.             }
  1478.             break;
  1479.         }
  1480.         case Rbegbuf:
  1481.         {
  1482.             opcode = Cbegbuf;
  1483.             goto store_opcode;
  1484.         }
  1485.         case Rendbuf:
  1486.         {
  1487.             opcode = Cendbuf;
  1488.             goto store_opcode;
  1489.         }
  1490.         case Rwordchar:
  1491.         {
  1492.             opcode = Csyntaxspec;
  1493.             ch = Sword;
  1494.             goto store_opcode_and_arg;
  1495.         }
  1496.         case Rnotwordchar:
  1497.         {
  1498.             opcode = Cnotsyntaxspec;
  1499.             ch = Sword;
  1500.             goto store_opcode_and_arg;
  1501.         }
  1502.         case Rwordbeg:
  1503.         {
  1504.             opcode = Cwordbeg;
  1505.             goto store_opcode;
  1506.         }
  1507.         case Rwordend:
  1508.         {
  1509.             opcode = Cwordend;
  1510.             goto store_opcode;
  1511.         }
  1512.         case Rwordbound:
  1513.         {
  1514.             opcode = Cwordbound;
  1515.             goto store_opcode;
  1516.         }
  1517.         case Rnotwordbound:
  1518.         {
  1519.             opcode = Cnotwordbound;
  1520.             goto store_opcode;
  1521.         }
  1522.         default:
  1523.         {
  1524.             abort();
  1525.         }
  1526.         }
  1527.         beginning_context = (op == Ropenpar || op == Ror);
  1528.     }
  1529.     if (starts_base != 0)
  1530.         goto parenthesis_error;
  1531.     assert(num_jumps == 0);
  1532.     ALLOC(1);
  1533.     STORE(Cend);
  1534.     SET_FIELDS;
  1535.     if(!re_optimize(bufp))
  1536.         return "Optimization error";
  1537.     return NULL;
  1538.  
  1539.   op_error:
  1540.     SET_FIELDS;
  1541.     return "Badly placed special character";
  1542.  
  1543.   bad_match_register:
  1544.     SET_FIELDS;
  1545.     return "Bad match register number";
  1546.    
  1547.   hex_error:
  1548.     SET_FIELDS;
  1549.     return "Bad hexadecimal number";
  1550.    
  1551.   parenthesis_error:
  1552.     SET_FIELDS;
  1553.     return "Badly placed parenthesis";
  1554.    
  1555.   out_of_memory:
  1556.     SET_FIELDS;
  1557.     return "Out of memory";
  1558.    
  1559.   ends_prematurely:
  1560.     SET_FIELDS;
  1561.     return "Regular expression ends prematurely";
  1562.  
  1563.   too_complex:
  1564.     SET_FIELDS;
  1565.     return "Regular expression too complex";
  1566. }
  1567.  
  1568. #undef CHARAT
  1569. #undef NEXTCHAR
  1570. #undef GETHEX
  1571. #undef ALLOC
  1572. #undef STORE
  1573. #undef CURRENT_LEVEL_START
  1574. #undef SET_LEVEL_START
  1575. #undef PUSH_LEVEL_STARTS
  1576. #undef POP_LEVEL_STARTS
  1577. #undef PUT_ADDR
  1578. #undef INSERT_JUMP
  1579. #undef SETBIT
  1580. #undef SET_FIELDS
  1581.  
  1582. #define PREFETCH if (text == textend) goto fail
  1583.  
  1584. #define NEXTCHAR(var) \
  1585. PREFETCH; \
  1586. var = (unsigned char)*text++; \
  1587. if (translate) \
  1588.     var = translate[var]
  1589.  
  1590. int re_match(bufp,
  1591.          string,
  1592.          size,
  1593.          pos,
  1594.          old_regs)
  1595.     regexp_t bufp;
  1596.     unsigned char *string;
  1597.     int size;
  1598.     int pos;
  1599.     regexp_registers_t old_regs;
  1600. {
  1601.     unsigned char *code;
  1602.     unsigned char *translate;
  1603.     unsigned char *text;
  1604.     unsigned char *textstart;
  1605.     unsigned char *textend;
  1606.     int a;
  1607.     int b;
  1608.     int ch;
  1609.     int reg;
  1610.     int match_end;
  1611.     unsigned char *regstart;
  1612.     unsigned char *regend;
  1613.     int regsize;
  1614.     match_state state;
  1615.   
  1616.     assert(pos >= 0 && size >= 0);
  1617.     assert(pos <= size);
  1618.   
  1619.     text = string + pos;
  1620.     textstart = string;
  1621.     textend = string + size;
  1622.   
  1623.     code = bufp->buffer;
  1624.   
  1625.     translate = bufp->translate;
  1626.   
  1627.     NEW_STATE(state, bufp->num_registers);
  1628.  
  1629.   continue_matching:
  1630.     switch (*code++)
  1631.     {
  1632.     case Cend:
  1633.     {
  1634.         match_end = text - textstart;
  1635.         if (old_regs)
  1636.         {
  1637.             old_regs->start[0] = pos;
  1638.             old_regs->end[0] = match_end;
  1639.             if (!bufp->uses_registers)
  1640.             {
  1641.                 for (a = 1; a < RE_NREGS; a++)
  1642.                 {
  1643.                     old_regs->start[a] = -1;
  1644.                     old_regs->end[a] = -1;
  1645.                 }
  1646.             }
  1647.             else
  1648.             {
  1649.                 for (a = 1; a < bufp->num_registers; a++)
  1650.                 {
  1651.                     if ((GET_REG_START(state, a) == NULL) ||
  1652.                         (GET_REG_END(state, a) == NULL))
  1653.                     {
  1654.                         old_regs->start[a] = -1;
  1655.                         old_regs->end[a] = -1;
  1656.                         continue;
  1657.                     }
  1658.                     old_regs->start[a] = GET_REG_START(state, a) - textstart;
  1659.                     old_regs->end[a] = GET_REG_END(state, a) - textstart;
  1660.                 }
  1661.                 for (; a < RE_NREGS; a++)
  1662.                 {
  1663.                     old_regs->start[a] = -1;
  1664.                     old_regs->end[a] = -1;
  1665.                 }
  1666.             }
  1667.         }
  1668.         FREE_STATE(state);
  1669.         return match_end - pos;
  1670.     }
  1671.     case Cbol:
  1672.     {
  1673.         if (text == textstart || text[-1] == '\n')
  1674.             goto continue_matching;
  1675.         goto fail;
  1676.     }
  1677.     case Ceol:
  1678.     {
  1679.         if (text == textend || *text == '\n')
  1680.             goto continue_matching;
  1681.         goto fail;
  1682.     }
  1683.     case Cset:
  1684.     {
  1685.         NEXTCHAR(ch);
  1686.         if (code[ch/8] & (1<<(ch & 7)))
  1687.         {
  1688.             code += 256/8;
  1689.             goto continue_matching;
  1690.         }
  1691.         goto fail;
  1692.     }
  1693.     case Cexact:
  1694.     {
  1695.         NEXTCHAR(ch);
  1696.         if (ch != (unsigned char)*code++)
  1697.             goto fail;
  1698.         goto continue_matching;
  1699.     }
  1700.     case Canychar:
  1701.     {
  1702.         NEXTCHAR(ch);
  1703.         if (ch == '\n')
  1704.             goto fail;
  1705.         goto continue_matching;
  1706.     }
  1707.     case Cstart_memory:
  1708.     {
  1709.         reg = *code++;
  1710.         SET_REG_START(state, reg, text, goto error);
  1711.         goto continue_matching;
  1712.     }
  1713.     case Cend_memory:
  1714.     {
  1715.         reg = *code++;
  1716.         SET_REG_END(state, reg, text, goto error);
  1717.         goto continue_matching;
  1718.     }
  1719.     case Cmatch_memory:
  1720.     {
  1721.         reg = *code++;
  1722.         regstart = GET_REG_START(state, reg);
  1723.         regend = GET_REG_END(state, reg);
  1724.         if ((regstart == NULL) || (regend == NULL))
  1725.             goto fail;  /* or should we just match nothing? */
  1726.         regsize = regend - regstart;
  1727.  
  1728.         if (regsize > (textend - text))
  1729.             goto fail;
  1730.         if(translate)
  1731.         {
  1732.             for (; regstart < regend; regstart++, text++)
  1733.                 if (translate[*regstart] != translate[*text])
  1734.                     goto fail;
  1735.         }
  1736.         else
  1737.             for (; regstart < regend; regstart++, text++)
  1738.                 if (*regstart != *text)
  1739.                     goto fail;
  1740.         goto continue_matching;
  1741.     }
  1742.     case Cupdate_failure_jump:
  1743.     {
  1744.         UPDATE_FAILURE(state, text, goto error);
  1745.         /* fall to next case */
  1746.     }
  1747.     /* treat Cstar_jump just like Cjump if it hasn't been optimized */
  1748.     case Cstar_jump:
  1749.     case Cjump:
  1750.     {
  1751.         a = (unsigned char)*code++;
  1752.         a |= (unsigned char)*code++ << 8;
  1753.         code += (int)SHORT(a);
  1754.         if (code<bufp->buffer || bufp->buffer+bufp->used<code) {
  1755.                 PyErr_SetString(PyExc_SystemError, "Regex VM jump out of bounds (Cjump)");
  1756.             FREE_STATE(state);
  1757.                         return -2;
  1758.              }
  1759.         goto continue_matching;
  1760.     }
  1761.     case Cdummy_failure_jump:
  1762.     {
  1763.                 unsigned char *failuredest;
  1764.       
  1765.         a = (unsigned char)*code++;
  1766.         a |= (unsigned char)*code++ << 8;
  1767.         a = (int)SHORT(a);
  1768.         assert(*code == Cfailure_jump);
  1769.         b = (unsigned char)code[1];
  1770.         b |= (unsigned char)code[2] << 8;
  1771.                 failuredest = code + (int)SHORT(b) + 3;
  1772.         if (failuredest<bufp->buffer || bufp->buffer+bufp->used < failuredest) {
  1773.                 PyErr_SetString(PyExc_SystemError, "Regex VM jump out of bounds (Cdummy_failure_jump failuredest)");
  1774.             FREE_STATE(state);
  1775.                         return -2;
  1776.         }
  1777.         PUSH_FAILURE(state, failuredest, NULL, goto error);
  1778.         code += a;
  1779.         if (code<bufp->buffer || bufp->buffer+bufp->used < code) {
  1780.                 PyErr_SetString(PyExc_SystemError, "Regex VM jump out of bounds (Cdummy_failure_jump code)");
  1781.             FREE_STATE(state);
  1782.                         return -2;
  1783.              }
  1784.         goto continue_matching;
  1785.     }
  1786.     case Cfailure_jump:
  1787.     {
  1788.         a = (unsigned char)*code++;
  1789.         a |= (unsigned char)*code++ << 8;
  1790.         a = (int)SHORT(a);
  1791.         if (code+a<bufp->buffer || bufp->buffer+bufp->used < code+a) {
  1792.                 PyErr_SetString(PyExc_SystemError, "Regex VM jump out of bounds (Cfailure_jump)");
  1793.             FREE_STATE(state);
  1794.                         return -2;
  1795.              }
  1796.         PUSH_FAILURE(state, code + a, text, goto error);
  1797.         goto continue_matching;
  1798.     }
  1799.     case Crepeat1:
  1800.     {
  1801.         unsigned char *pinst;
  1802.         a = (unsigned char)*code++;
  1803.         a |= (unsigned char)*code++ << 8;
  1804.         a = (int)SHORT(a);
  1805.         pinst = code + a;
  1806.         if (pinst<bufp->buffer || bufp->buffer+bufp->used<pinst) {
  1807.                 PyErr_SetString(PyExc_SystemError, "Regex VM jump out of bounds (Crepeat1)");
  1808.             FREE_STATE(state);
  1809.                         return -2;
  1810.              }
  1811.         /* pinst is sole instruction in loop, and it matches a
  1812.          * single character.  Since Crepeat1 was originally a
  1813.          * Cupdate_failure_jump, we also know that backtracking
  1814.          * is useless: so long as the single-character
  1815.          * expression matches, it must be used.  Also, in the
  1816.          * case of +, we've already matched one character, so +
  1817.          * can't fail: nothing here can cause a failure.  */
  1818.         switch (*pinst++)
  1819.         {
  1820.         case Cset:
  1821.           {
  1822.                 if (translate)
  1823.             {
  1824.                 while (text < textend)
  1825.                 {
  1826.                     ch = translate[(unsigned char)*text];
  1827.                     if (pinst[ch/8] & (1<<(ch & 7)))
  1828.                         text++;
  1829.                     else
  1830.                         break;
  1831.                 }
  1832.             }
  1833.             else
  1834.             {
  1835.                 while (text < textend)
  1836.                 {
  1837.                     ch = (unsigned char)*text;
  1838.                     if (pinst[ch/8] & (1<<(ch & 7)))
  1839.                         text++;
  1840.                     else
  1841.                         break;
  1842.                 }
  1843.             }
  1844.             break;
  1845.                 }
  1846.         case Cexact:
  1847.         {
  1848.             ch = (unsigned char)*pinst;
  1849.             if (translate)
  1850.             {
  1851.                 while (text < textend &&
  1852.                        translate[(unsigned char)*text] == ch)
  1853.                     text++;
  1854.             }
  1855.             else
  1856.             {
  1857.                 while (text < textend && (unsigned char)*text == ch)
  1858.                     text++;
  1859.             }
  1860.             break;
  1861.         }
  1862.         case Canychar:
  1863.         {
  1864.             while (text < textend && (unsigned char)*text != '\n')
  1865.                 text++;
  1866.             break;
  1867.         }
  1868.         case Csyntaxspec:
  1869.         {
  1870.             a = (unsigned char)*pinst;
  1871.             if (translate)
  1872.             {
  1873.                 while (text < textend &&
  1874.                        (SYNTAX(translate[*text]) & a) )
  1875.                     text++;
  1876.             }
  1877.             else
  1878.             {
  1879.                 while (text < textend && (SYNTAX(*text) & a) )
  1880.                     text++;
  1881.             }
  1882.             break;
  1883.         }
  1884.         case Cnotsyntaxspec:
  1885.         {
  1886.             a = (unsigned char)*pinst;
  1887.             if (translate)
  1888.             {
  1889.                 while (text < textend &&
  1890.                        !(SYNTAX(translate[*text]) & a) )
  1891.                     text++;
  1892.             }
  1893.             else
  1894.             {
  1895.                 while (text < textend && !(SYNTAX(*text) & a) )
  1896.                     text++;
  1897.             }
  1898.             break;
  1899.         }
  1900.         default:
  1901.         {
  1902.                 FREE_STATE(state);
  1903.                 PyErr_SetString(PyExc_SystemError, "Unknown regex opcode: memory corrupted?");
  1904.                 return -2;
  1905.             /*NOTREACHED*/
  1906.         }
  1907.         }
  1908.         /* due to the funky way + and * are compiled, the top
  1909.          * failure- stack entry at this point is actually a
  1910.          * success entry -- update it & pop it */
  1911.         UPDATE_FAILURE(state, text, goto error);
  1912.         goto fail;      /* i.e., succeed <wink/sigh> */
  1913.     }
  1914.     case Cbegbuf:
  1915.     {
  1916.         if (text == textstart)
  1917.             goto continue_matching;
  1918.         goto fail;
  1919.     }
  1920.     case Cendbuf:
  1921.     {
  1922.         if (text == textend)
  1923.             goto continue_matching;
  1924.         goto fail;
  1925.     }
  1926.     case Cwordbeg:
  1927.     {
  1928.         if (text == textend)
  1929.             goto fail;
  1930.         if (!(SYNTAX(*text) & Sword)) 
  1931.             goto fail;
  1932.         if (text == textstart)
  1933.             goto continue_matching;
  1934.         if (!(SYNTAX(text[-1]) & Sword))
  1935.             goto continue_matching;
  1936.         goto fail;
  1937.     }
  1938.     case Cwordend:
  1939.     {
  1940.         if (text == textstart)
  1941.             goto fail;
  1942.         if (!(SYNTAX(text[-1]) & Sword))
  1943.             goto fail;
  1944.         if (text == textend)
  1945.             goto continue_matching;
  1946.         if (!(SYNTAX(*text) & Sword))
  1947.                 goto continue_matching;
  1948.                 goto fail;
  1949.     }
  1950.     case Cwordbound:
  1951.     {
  1952.         /* Note: as in gnu regexp, this also matches at the
  1953.          * beginning and end of buffer.  */
  1954.  
  1955.         if (text == textstart || text == textend)
  1956.             goto continue_matching;
  1957.         if ((SYNTAX(text[-1]) & Sword) ^ (SYNTAX(*text) & Sword))
  1958.             goto continue_matching;
  1959.         goto fail;
  1960.     }
  1961.     case Cnotwordbound:
  1962.     {
  1963.         /* Note: as in gnu regexp, this never matches at the
  1964.          * beginning and end of buffer.  */
  1965.         if (text == textstart || text == textend)
  1966.             goto fail;
  1967.         if (!((SYNTAX(text[-1]) & Sword) ^ (SYNTAX(*text) & Sword)))
  1968.                       goto continue_matching;
  1969.         goto fail;
  1970.     }
  1971.     case Csyntaxspec:
  1972.     {
  1973.         NEXTCHAR(ch);
  1974.         if (!(SYNTAX(ch) & (unsigned char)*code++))
  1975.             goto fail;
  1976.         goto continue_matching;
  1977.     }
  1978.     case Cnotsyntaxspec:
  1979.     {
  1980.         NEXTCHAR(ch);
  1981.         if (SYNTAX(ch) & (unsigned char)*code++)
  1982.             goto fail;
  1983.         goto continue_matching;
  1984.     }
  1985.     default:
  1986.     {
  1987.             FREE_STATE(state);
  1988.             PyErr_SetString(PyExc_SystemError, "Unknown regex opcode: memory corrupted?");
  1989.         return -2;
  1990.         /*NOTREACHED*/
  1991.     }
  1992.     }
  1993.     
  1994.     
  1995.  
  1996. #if 0 /* This line is never reached --Guido */
  1997.     abort();
  1998. #endif
  1999.     /*
  2000.      *NOTREACHED
  2001.      */
  2002.  
  2003.     /* Using "break;" in the above switch statement is equivalent to "goto fail;" */
  2004.   fail:
  2005.     POP_FAILURE(state, code, text, goto done_matching, goto error);
  2006.     goto continue_matching;
  2007.   
  2008.   done_matching:
  2009. /*   if(translated != NULL) */
  2010. /*      free(translated); */
  2011.     FREE_STATE(state);
  2012.     return -1;
  2013.  
  2014.   error:
  2015. /*   if (translated != NULL) */
  2016. /*      free(translated); */
  2017.     FREE_STATE(state);
  2018.     return -2;
  2019. }
  2020.     
  2021.  
  2022. #undef PREFETCH
  2023. #undef NEXTCHAR
  2024.  
  2025. int re_search(bufp,
  2026.           string,
  2027.           size,
  2028.           pos,
  2029.           range,
  2030.           regs)
  2031.     regexp_t bufp;
  2032.     unsigned char *string;
  2033.     int size;
  2034.     int pos;
  2035.     int range;
  2036.     regexp_registers_t regs;
  2037. {
  2038.     unsigned char *fastmap;
  2039.     unsigned char *translate;
  2040.     unsigned char *text;
  2041.     unsigned char *partstart;
  2042.     unsigned char *partend;
  2043.     int dir;
  2044.     int ret;
  2045.     unsigned char anchor;
  2046.   
  2047.     assert(size >= 0 && pos >= 0);
  2048.     assert(pos + range >= 0 && pos + range <= size); /* Bugfix by ylo */
  2049.   
  2050.     fastmap = bufp->fastmap;
  2051.     translate = bufp->translate;
  2052.     if (fastmap && !bufp->fastmap_accurate) {
  2053.                 re_compile_fastmap(bufp);
  2054.             if (PyErr_Occurred()) return -2;
  2055.     }
  2056.     
  2057.     anchor = bufp->anchor;
  2058.     if (bufp->can_be_null == 1) /* can_be_null == 2: can match null at eob */
  2059.         fastmap = NULL;
  2060.  
  2061.     if (range < 0)
  2062.     {
  2063.         dir = -1;
  2064.         range = -range;
  2065.     }
  2066.     else
  2067.         dir = 1;
  2068.  
  2069.     if (anchor == 2) {
  2070.         if (pos != 0)
  2071.             return -1;
  2072.         else
  2073.             range = 0;
  2074.     }
  2075.  
  2076.     for (; range >= 0; range--, pos += dir)
  2077.     {
  2078.         if (fastmap)
  2079.         {
  2080.             if (dir == 1)
  2081.             { /* searching forwards */
  2082.  
  2083.                 text = string + pos;
  2084.                 partend = string + size;
  2085.                 partstart = text;
  2086.                 if (translate)
  2087.                     while (text != partend &&
  2088.                            !fastmap[(unsigned char) translate[(unsigned char)*text]])
  2089.                         text++;
  2090.                 else
  2091.                     while (text != partend && !fastmap[(unsigned char)*text])
  2092.                         text++;
  2093.                 pos += text - partstart;
  2094.                 range -= text - partstart;
  2095.                 if (pos == size && bufp->can_be_null == 0)
  2096.                     return -1;
  2097.             }
  2098.             else
  2099.             { /* searching backwards */
  2100.                 text = string + pos;
  2101.                 partstart = string + pos - range;
  2102.                 partend = text;
  2103.                 if (translate)
  2104.                     while (text != partstart &&
  2105.                            !fastmap[(unsigned char)
  2106.                                translate[(unsigned char)*text]])
  2107.                         text--;
  2108.                 else
  2109.                     while (text != partstart &&
  2110.                            !fastmap[(unsigned char)*text])
  2111.                         text--;
  2112.                 pos -= partend - text;
  2113.                 range -= partend - text;
  2114.             }
  2115.         }
  2116.         if (anchor == 1)
  2117.         { /* anchored to begline */
  2118.             if (pos > 0 && (string[pos - 1] != '\n'))
  2119.                 continue;
  2120.         }
  2121.         assert(pos >= 0 && pos <= size);
  2122.         ret = re_match(bufp, string, size, pos, regs);
  2123.         if (ret >= 0)
  2124.             return pos;
  2125.         if (ret == -2)
  2126.             return -2;
  2127.     }
  2128.     return -1;
  2129. }
  2130.  
  2131. /*
  2132. ** Local Variables:
  2133. ** mode: c
  2134. ** c-file-style: "python"
  2135. ** End:
  2136. */
  2137.